//
//  RSWRESTService+Workflow.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 2017/8/25.
//  Copyright © 2017年 com.xtion.crm. All rights reserved.
//

#import "RSWRESTService+Workflow.h"

@implementation RSWRESTService (Workflow)

-(XLGResult*)getALlWorkflowDate:(NSString*)date
                          token:(NSString*)token
                      pageIndex:(NSInteger)pageIndex
                       pageSize:(NSInteger)pageSize
                           code:(NSString*)code
{
    NSString *URI = [NSString stringWithFormat:@"api/v5/Slickflow/ListByAll?date=%@&subject=%@&size=%@&page=%@&token=%@&code=%@",
                     date,@"", @(pageSize), @(pageIndex), token, code];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypeGet];
    
    if(r.outResult == YES)
    {
        NSArray *list = r.outObj[@"Value"];
        
        XMSQueueDictionary *qdSet = [WorkflowAllListInfo JSONObjectProperty:nil];
        list = [CTransformHTTPRESTObj JSONObjs:list transformToProtocolObj:[WorkflowAllListInfo class] accordingToColPropertyPair:qdSet];
        
        NSArray *dateList = r.outObj[@"DateList"];
        qdSet = [WorkflowDateListInfo JSONObjectProperty:nil];
        dateList = [CTransformHTTPRESTObj JSONObjs:dateList transformToProtocolObj:[WorkflowDateListInfo class] accordingToColPropertyPair:qdSet];
        
        XMSQueueDictionary *dic = [XMSQueueDictionary new];
        
        [dic setValue:list forKey:@"List"];
        [dic setValue:dateList forKey:@"DateList"];
        
        r = [XLGResult resultWithOutResult:YES outObj:dic];
    }
    return r;
}

-(XLGResult*)getToDoWorkflowDate:(NSString*)date
                          token:(NSString*)token
                      pageIndex:(NSInteger)pageIndex
                       pageSize:(NSInteger)pageSize
                           code:(NSString*)code
{
    NSString *URI = [NSString stringWithFormat:@"api/v5/Slickflow/ListByToDo?date=%@&subject=%@&size=%@&page=%@&token=%@&code=%@",
                     date,@"", @(pageSize), @(pageIndex), token, code];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypeGet];
    
    if(r.outResult == YES)
    {
        NSArray *list = r.outObj[@"Value"];
        
        XMSQueueDictionary *qdSet = [WorkflowToDoListInfo JSONObjectProperty:nil];
        list = [CTransformHTTPRESTObj JSONObjs:list transformToProtocolObj:[WorkflowToDoListInfo class] accordingToColPropertyPair:qdSet];
        
        NSArray *dateList = r.outObj[@"DateList"];
        qdSet = [WorkflowDateListInfo JSONObjectProperty:nil];
        dateList = [CTransformHTTPRESTObj JSONObjs:dateList transformToProtocolObj:[WorkflowDateListInfo class] accordingToColPropertyPair:qdSet];
        
        XMSQueueDictionary *dic = [XMSQueueDictionary new];
        
        [dic setValue:list forKey:@"List"];
        [dic setValue:dateList forKey:@"DateList"];
        
        r = [XLGResult resultWithOutResult:YES outObj:dic];
    }
    return r;
}

-(XLGResult*)getWorkflowApply:(NSString*)processcode
                        token:(NSString*)token
                    customform:(NSString*)customform
                        version:(NSString*)version
{
    NSString *URI = [NSString stringWithFormat:@"api/v5/Slickflow/Apply?processcode=%@&customform=%@&token=%@&version=%@",
                     processcode,customform, token, version];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypeGet];
    
    if(r.outResult == YES)
    {
        id value = r.outObj[@"Value"];
        
        r = [XLGResult resultWithOutResult:YES outObj:value];
    }
    return r;
}

-(XLGResult*)getWorkflowApprove:(NSString*)processcode
                          token:(NSString*)token
                     customform:(NSString*)customform
                        version:(NSString*)version
                     instanceid:(NSString*)instanceid
{
    NSString *URI = [NSString stringWithFormat:
                     @"api/v5/Slickflow/Approve?processcode=%@&customform=%@&token=%@&version=%@&instanceid=%@",
                     processcode, customform, token, version, instanceid];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypeGet];
    
    if(r.outResult == YES)
    {
        id value = r.outObj[@"Value"];
        
        r = [XLGResult resultWithOutResult:YES outObj:value];
    }
    return r;
}

-(XLGResult*)getStepRoleUserTree:(NSString*)processcode
                             token:(NSString*)token
                           version:(NSString*)version
                               dic:(NSDictionary*)dic
                               url:(NSString*)url
{
    NSString *URI = [NSString stringWithFormat:@"api/v5/Slickflow/%@?processcode=%@&token=%@&version=%@",
                     url, processcode, token, version];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:dic HTTPMethod:XLGHTTPMethodTypePost];
    
    if(r.outResult == YES)
    {
        id value = r.outObj[@"Value"];
        
        r = [XLGResult resultWithOutResult:YES outObj:value];
    }
    return r;
}

-(XLGResult*)startWorkflowAppInstance:(NSString*)processcode
                                token:(NSString*)token
                              version:(NSString*)version
                                  dic:(NSDictionary*)dic
{
    NSString *URI = [NSString stringWithFormat:@"api/v5/Slickflow/Start?processcode=%@&token=%@&version=%@",
                     processcode, token, version];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:dic HTTPMethod:XLGHTTPMethodTypePost];
    
    if(r.outResult == YES && r.outObj != nil)
    {
        NSInteger success = [r.outObj[@"Success"] integerValue];
        
        if(success == YES)
        {
            return [XLGResult resultWithOutResult:r.outResult outObj:@(YES)];
        }
        else
        {
            NSError *error = r.outError;
            if(error == nil)
                error = [NSError errorWithDomain:r.outObj code:0 userInfo:nil];
            
            return [XLGResult resultWithOutResult:r.outResult outObj:@(NO) outError:error];
        }
    }
    return r;
}

-(XLGResult*)nextWorkflowAppInstance:(NSString*)processcode
                                token:(NSString*)token
                              version:(NSString*)version
                                  dic:(NSDictionary*)dic
{
    NSString *URI = [NSString stringWithFormat:@"api/v5/Slickflow/Next?processcode=%@&token=%@&version=%@",
                     processcode, token, version];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:dic HTTPMethod:XLGHTTPMethodTypePost];
    
    if(r.outResult == YES && r.outObj != nil)
    {
        NSInteger success = [r.outObj[@"Success"] integerValue];
        
        if(success == YES)
        {
            return [XLGResult resultWithOutResult:r.outResult outObj:@(YES)];
        }
        else
        {
            NSError *error = r.outError;
            if(error == nil)
                error = [NSError errorWithDomain:r.outObj code:0 userInfo:nil];
            
            return [XLGResult resultWithOutResult:r.outResult outObj:@(NO) outError:error];
        }
    }
    return r;
}

-(XLGResult*)withDrawWorkflowAppInstance:(NSString*)processcode
                                   token:(NSString*)token
                                 version:(NSString*)version
                                 comment:(NSString*)comment
                                 modelid:(NSDictionary*)modelid
{
    NSString *URI = [NSString stringWithFormat:
                     @"api/v5/Slickflow/Withdraw?processcode=%@&token=%@&version=%@&comment=%@&modelid=%@",
                     processcode, token, version, comment, modelid];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypePost];
    
    if(r.outResult == YES && r.outObj != nil)
    {
        NSInteger success = [r.outObj[@"Success"] integerValue];
        
        if(success == YES)
        {
            return [XLGResult resultWithOutResult:r.outResult outObj:@(YES)];
        }
        else
        {
            NSError *error = r.outError;
            if(error == nil)
                error = [NSError errorWithDomain:r.outObj code:0 userInfo:nil];
            
            return [XLGResult resultWithOutResult:r.outResult outObj:@(NO) outError:error];
        }
    }
    return r;
}

-(XLGResult*)sendBackWorkflowAppInstance:(NSString*)processcode
                                   token:(NSString*)token
                                 version:(NSString*)version
                                 comment:(NSString*)comment
                                 modelid:(NSString*)modelid
{
    NSString *URI = [NSString stringWithFormat:
                     @"api/v5/Slickflow/SendBack?processcode=%@&token=%@&version=%@&comment=%@&modelid=%@",
                     processcode, token, version, comment, modelid];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypePost];
    
    if(r.outResult == YES && r.outObj != nil)
    {
        NSInteger success = [r.outObj[@"Success"] integerValue];
        
        if(success == YES)
        {
            return [XLGResult resultWithOutResult:r.outResult outObj:@(YES)];
        }
        else
        {
            NSError *error = r.outError;
            if(error == nil)
                error = [NSError errorWithDomain:r.outObj code:0 userInfo:nil];
            
            return [XLGResult resultWithOutResult:r.outResult outObj:@(NO) outError:error];
        }
    }
    return r;
}

-(XLGResult*)discardWorkflowAppInstance:(NSString*)processcode
                                   token:(NSString*)token
                                 version:(NSString*)version
                                 comment:(NSString*)comment
                                 modelid:(NSString*)modelid
{
    NSString *URI = [NSString stringWithFormat:
                     @"api/v5/Slickflow/Discard?processcode=%@&token=%@&version=%@&comment=%@&modelid=%@",
                     processcode, token, version, comment, modelid];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypePost];
    
    if(r.outResult == YES && r.outObj != nil)
    {
        NSInteger success = [r.outObj[@"Success"] integerValue];
        
        if(success == YES)
        {
            return [XLGResult resultWithOutResult:r.outResult outObj:@(YES)];
        }
        else
        {
            NSError *error = r.outError;
            if(error == nil)
                error = [NSError errorWithDomain:r.outObj code:0 userInfo:nil];
            
            return [XLGResult resultWithOutResult:r.outResult outObj:@(NO) outError:error];
        }
    }
    return r;
}

-(XLGResult*)reverseWorkflowAppInstance:(NSString*)processcode
                                  token:(NSString*)token
                                version:(NSString*)version
                                comment:(NSString*)comment
                                modelid:(NSString*)modelid
{
    NSString *URI = [NSString stringWithFormat:
                     @"api/v5/Slickflow/Reverse?processcode=%@&token=%@&version=%@&comment=%@&modelid=%@",
                     processcode, token, version, comment, modelid];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypePost];
    
    if(r.outResult == YES && r.outObj != nil)
    {
        NSInteger success = [r.outObj[@"Success"] integerValue];
        
        if(success == YES)
        {
            return [XLGResult resultWithOutResult:r.outResult outObj:@(YES)];
        }
        else
        {
            NSError *error = r.outError;
            if(error == nil)
                error = [NSError errorWithDomain:r.outObj code:0 userInfo:nil];
            
            return [XLGResult resultWithOutResult:r.outResult outObj:@(NO) outError:error];
        }
    }
    return r;
}

-(XLGResult*)getWorkflowHistoryModelId:(NSString*)modelId token:(NSString*)token
{
    NSString *URI = [NSString stringWithFormat:@"api/v5/slickflow/ListByFlowInfo?token=%@&modelid=%@",
                    token, modelId];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypeGet];
    
    if(r.outResult == YES)
    {
        NSArray *list = r.outObj[@"Value"];
        
        XMSQueueDictionary *qdSet = [WorkflowHistoryInfo JSONObjectProperty:nil];
        list = [CTransformHTTPRESTObj JSONObjs:list transformToProtocolObj:[WorkflowHistoryInfo class] accordingToColPropertyPair:qdSet];
        
        r = [XLGResult resultWithOutResult:YES outObj:list];
    }
    return r;
}
@end
